<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * home.php
 * @author ranuandra
 *
 * 24 Apr 2012 14:14:57
 *
 * 
 */

class Controller_Admin_Home extends Kohana_Admin
{
	public $template = 'admin/login';
	public function action_index()
	{
/*		try 
		{
			$_POST['username'] = "test";
			$_POST['password'] = "12345678";
			$_POST['password_confirm'] = "12345678";
			$_POST['email'] = "adi@email.com";
			
			
			
			$user = ORM::factory('user');
			
			$user->create_user($_POST, array(
		 										'username',
												'password',
		 										'email',
		  									));
	  		$login_role = new Model_Role(array('name' =>'login'));
			$user->add('roles',$login_role);
	  		$login_role = new Model_Role(array('name' =>'admin'));
			$user->add('roles',$login_role);
			
		}
		catch (ORM_Validation_Exception $e) {
			$errors = $e->errors('models');
			print_r($errors);
		}
		
		exit;*/
		
		if (Auth_ORM::instance()->logged_in() )
		{

			Request::current()->redirect(URL::base().'admin/hrd');

		}	
			
	}
	
	public function action_login()
	{
		if(isset($_POST['username']) and isset($_POST['password']))
		{
			$user = Auth_ORM::instance();
			
			$status = Auth_ORM::instance()->login($_POST['username'], $_POST['password'], FALSE);
			if($status==1)
			{
				Request::current()->redirect(URL::base()."admin/hrd");
			}
			else 
			{
				Request::current()->redirect(URL::base()."admin");
			}
			
				
		}
		else 
		{
			Request::current()->redirect(URL::base()."admin");
		}		
	}
	
	public function action_logout()
	{
		Auth_ORM::instance()->logout();
		echo "<script>window.location.replace(\"".URL::base()."admin\")</script>";
		exit;
	}
}